<template>
	<view class="imagePage">
		<!-- <image :src="img_hb" mode=""></image> -->
		<!-- 推荐好友 -->
		<view class="recommend">
			<image class="codeTwo" :src="img_hb" mode=""></image>
			<view class="title">
				<view class="">无需下载安装扫码即可使用</view>
				<view class="">没有激活费用人人均可扫码变现</view>
				<view class="">点击下方按钮后长按图片可转发或保存</view>
			</view>
			
			<!-- src="/static/my/dz.png" -->
			<!-- <image :src="image+'assets/web_img/p2.png'" mode=""></image> -->
			<!-- <button class="invitation btn" @tap.stop="shareClick" open-type="share" >发送好友</button> -->
			<view class="invitation btn" @tap.stop="downloadImage">保存分享</view>
		</view>
		
		<!-- pk胜 -->
		<view class="PKWin" v-if="type == 2">
			<image :src="image+'assets/web_img/edit1.png'" mode=""></image></image>
			<view class="btn" v-if="isType != 2"  @tap.stop="goShop">去点餐</view>
		</view>
		
		<!-- pk负 -->
		<view class="PKNegative" v-if="type == 3">
			<image :src="image+'assets/web_img/edit17.png'" mode=""></image>
			<view class="btn" v-if="isType != 2" @tap.stop="comeAgain">再来一次</view>
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image: this.$base.smallimgUrl,
				type: 1, // type  1代表是 推荐好友  2代表是 pk胜  3代表是  pk负
				pageData: '',
				userInfo: uni.getStorageSync('userInfo'),
				share: {},
				isType:0,
				img_hb:''
			};
		},
		onLoad(el) {
			this.type = el.type
			this.isType = el.isType
			if(this.type == 1){
				this.getPageData()
			}
		},
		onShareAppMessage() {
			return {
				title: this.share.title,
				path: this.share.path,
				imageUrl: this.share.imageUrl,
				desc: this.share.desc,
				content: this.share.content,
				success(res) {
					this.$utils.toast('分享成功')
					this.share = {}
					console.log(res)
				},
				fail(res) {
					this.$utils.toast('分享失败')
				}
			}
		},
		methods: {
			getPageData(){
				let that = this
				that.$api.shareQRCode().then(res => {
					// that.pageData = res.data.data.img
		            let url =   'data:image/png;base64,'+wx.arrayBufferToBase64(res.data)
					that.img_hb = url
				})
			},


			// 点击下载图片
			downloadImage(){
				var save = wx.getFileSystemManager(); // 获取文件管理器对象
				var number = Math.random();
				save.writeFile({
				  filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png', // 表示生成一个临时文件名
				  data: this.img_hb.split("data:image/png;base64")[1] ,
				  encoding: 'base64',
				  success: res => {
					wx.saveImageToPhotosAlbum({
					  filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
					  success: function (res) {
						  console.log(res);
						wx.showToast({
						  title: '保存成功',
						})
					  },
					  fail: function (err) {
						console.log(err,'err1')
					  }
					})
					console.log(res,'res')
				  }, fail: err => {
					console.log(err,'err2')
				  }
				})

				// uni.downloadFile({
				// 	url: that.img_hb,
				// 	success(res) {
				// 		let tempFilePath = res.tempFilePath
				// 		uni.saveImageToPhotosAlbum({
				// 			filePath: tempFilePath,
				// 			success(res1){
				// 				that.$utils.toast('图片生成成功')
				// 			},
				// 			fail(err){
				// 				that.$utils.toast('图片生成失败')
				// 			}
				// 		})
				// 	}
				// })
			},
			// 点击确定，发给好友
			shareClick(){
				// console.log(this.userInfo.id,'this.userInfo.id')
				this.share = {
					title: '',
					path: `/pages/tabBar/home/home?inviter_id=${this.userInfo.id}`,
					imageUrl: '',
					desc: '',
					content: ''
				}
			},
			
			// 点击跳转点餐
			goShop(){
				uni.switchTab({
					url: '/pages/tabBar/shop/shop'
				})
			},
			
			// 再次发起pk
			comeAgain(){
				let that = this
				that.$api.pkIndexCreate().then(res => {
					console.log(res.data.data.active_id,'再来一次')
					uni.redirectTo({
						url: '/pages/pk/pk?active_id=' + res.data.data.active_id
					})
				})
			}
			
		}
	}
</script>

<style lang="less">
	.imagePage{
		.recommend{
			width: 100vw;
			height: 100vh;
			.title{
				position: absolute;
				width: 500rpx;
				top: 50%;
				left: 18%;
				text-align: center;
				view{
					height: 50rpx;
					line-height: 50rpx;
					font-size: 28rpx;
				}
			}
			image{
				width: 100%;
				height: 100%;
			}
			.codeTwo{
				width: 480rpx;
				height: 480rpx;
				position: absolute;
				top: 8%;
				left: 18%;
			}
			.btn{
				width: 80%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border-radius: 16rpx;
				font-size: 28rpx;
				font-weight: 500;
				position: fixed;
			}
			.invitation{
				background: #1FC064;
				color: #fff;
				bottom: 200rpx;
				left: 50%;
				transform: translateX(-50%);
			}
			.generate{
				background: #DCDCDC;
				color: #7F8084;
				bottom: 70rpx;
				left: 50%;
				transform: translateX(-50%);
			}
		}
		
		.PKWin,.PKNegative{
			width: 100vw;
			height: 100vh;
			image{
				width: 100%;
				height: 100%;
			}
			.btn{
				width: 80%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border-radius: 96rpx;
				font-size: 28rpx;
				font-weight: 500;
				position: fixed;
				background: #000;
				color: #fff;
				bottom: 240rpx;
				left: 50%;
				transform: translateX(-50%);
			}
		}

	}
</style>
